﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using System.Threading;
using ServiceStack.Redis.Generic;

namespace Zeus.Cache.Redis.Demo
{
    public class SimpleRedisDemo
    {
        private string host = "localhost";

        public void SimpleDemo()
        {
            string elementKey = "testKeyRedis";

            using (RedisClient redisClient = new RedisClient(host))
            {
                if (redisClient.Get<string>(elementKey) == null)
                {
                    // adding delay to see the difference
                    Thread.Sleep(2000);
                    // save value in cache
                    redisClient.Set(elementKey, "default value");
                }

                //change the value
                redisClient.Set(elementKey, "fuck you value");

                // get value from the cache by key
                string message = "Item value is: " + redisClient.Get<string>(elementKey);

                Console.WriteLine(message);
            }
        }

        public void EntityDemo()
        {
            using (RedisClient redisClient = new RedisClient(host))
            {
                IRedisTypedClient<Phone> phones = redisClient.As<Phone>();
                Phone phoneFive = phones.GetValue("5");

                if (phoneFive == null)
                {
                    // make a small delay
                    Thread.Sleep(2000);
                    // creating a new Phone entry
                    phoneFive = new Phone
                    {
                        Id = 5,
                        Manufacturer = "Nokia",
                        Model = "Lumia 200",
                        Owner = new Person
                        {
                            Id = 1,
                            Age = 90,
                            Name = "OldOne",
                            Profession = "sportsmen",
                            Surname = "OldManSurname"
                        }
                    };

                    // adding Entry to the typed entity set
                    phones.SetEntry(phoneFive.Id.ToString(), phoneFive);
                }

                string message = "Phone model is " + phoneFive.Manufacturer;
                message += "\nPhone Owner Name is: " + phoneFive.Owner.Name;

                Console.WriteLine(message);
            }
        }
    }
}




